package com.sy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sy.constant.MessageConstant;
import com.sy.constant.StatusConstant;
import com.sy.dto.UserLoginDTO;
import com.sy.dto.UserLoginDTO02;
import com.sy.dto.UserPageQueryDTO;
import com.sy.mapper.UserMapper;
import com.sy.pojo.Employee;
import com.sy.pojo.User;
import com.sy.result.PageResult;
import com.sy.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public PageResult findAll(UserPageQueryDTO userPageQueryDTO) {
        //根据用户名姓名模糊查询
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        //如果empDTO.getName()不为空那么，加入模糊查询条件
        queryWrapper.like(StringUtils.isNotEmpty(userPageQueryDTO.getName()), User::getName, userPageQueryDTO.getName())
                .select(User.class, info -> !info.getColumn().equals("password"));
        //配置分页信息，进行分页查询
        Page<User> page = this.page(
                new Page<>(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize()), queryWrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        //1.根据手机号查询用户
        queryWrapper.eq(User::getPhone,userLoginDTO.getPhone());
        User user=this.getOne(queryWrapper);
        if(user==null){
            throw new RuntimeException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //2.前台的获取到的密码进行加密
        String password= DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        //密码对比
        if(!password.equals(user.getPassword())){
            throw new RuntimeException(MessageConstant.PASSWORD_ERROR);
        }
        //3.判断账户是否被锁定
        if(user.getStatus()== StatusConstant.DISABLE){
            throw new RuntimeException(MessageConstant.ACCOUNT_LOCKED);
        }
        //4.判断账户是否被锁定
        if(user.getIsDelete()==StatusConstant.DISABLE){
            throw new RuntimeException("你的账户已经被注销");
        }
        return user;
    }

    @Override
    public User login02(UserLoginDTO02 userLoginDTO02) {
        //查找手机号用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,userLoginDTO02.getPhone());
        User user = this.getOne(queryWrapper);
        //判断user是否为空
        if(user==null){
            //该手机号没有注册
//            throw new RuntimeException(MessageConstant.ACCOUNT_NOT_FOUND);
            user=new User();
            user.setPhone(userLoginDTO02.getPhone());
            //实现保存
            this.save(user);
        }
        //判断手机号是否被禁用
        if(user.getStatus()==StatusConstant.DISABLE){
            throw new RuntimeException(MessageConstant.ACCOUNT_LOCKED);
        }
        //判断当前用户是否注销
        if(user.getIsDelete()==StatusConstant.DISABLE){
            throw new RuntimeException(MessageConstant.ACCOUNT_LOG_OFF);
        }
        return user;
    }
}
